import java.io.*;
import java.util.Scanner;

public class FileEncryptionAndDecryption {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        System.out.print("请用户输入一个源文件名（未加密文件A）：");
        String unencrypted = scan.nextLine();
        System.out.print("请用户输入一个输出文件名（加密文件B）：");
        String encryption = scan.nextLine();

        File unencryptedFile = new File("a.txt");
        if (!unencryptedFile.exists()) {
            System.out.println("未加密文件 " + unencryptedFile
                    + " 不存在");
            System.exit(1);
        }

        File encryptionFile = new File("b.txt");
        if (encryptionFile.exists()) {
            System.out.println("加密文件 " + encryptionFile
                    + " 已经存在");
            System.exit(2);
        }

        try (
                BufferedInputStream input = new BufferedInputStream(new FileInputStream(unencryptedFile));
                BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(encryptionFile));
        ) {
            int r;
            while ((r = input.read()) != -1) {
                int n;
                if ((byte)r+5>255){
                    n = (byte)r-256;
                }else {
                    n = (byte)r+5;
                }
                output.write(n);
            }
        }
        try(DataInputStream input2 = new DataInputStream(new FileInputStream(encryptionFile));)
        {
            int value;
            while ((value = input2.read()) != -1){
                int m;
                if ((byte)value-5<0){
                    m = (byte)value+256;
                }else {
                    m = (byte)value-5;
                }
                System.out.print((char) m);
            }
        }

    }
}
